<template>
  <!-- 头像区域 -->
  <div class="login_herd">
    <div class="login_box">
      <div class="avater_box">
        <img src="../assets/huang.jpg" />
      </div>
      <!-- 表单区域 -->
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        class="login_from"
      >
        <el-form-item label="用户" prop="username">
          <el-input v-model="form.username" prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-edit"
            type="password"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" round @click="login">登入</el-button>
          <el-button type="info" plain>注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          //是否启用验证     验证提示信息                   当鼠标离开文本框的时候
          { required: true, message: "请输入正确的用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const { data: returndate } = await this.$http.put(
            "login",
            this.form
          );
          if (returndate.meta.status !== 200)
            return this.$message.error("error: " + returndate.meta.msg);

          this.$message({
            message: "success:" + returndate.meta.msg,
            type: "success",
          });
          // console.log(returndate);
          //和 sessionStorage 属性允许在浏览器中存储 key/value 对的数据。
          //把token保存到sessionStorage
          window.sessionStorage.setItem("token", returndate.data.token);
          this.$router.push("/home");
        }
      });
    },
  },
};
</script>


<style lang="less" scoped>
.login_herd {
  background-color: rgb(98, 98, 216);
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;
  padding: 10px;
  box-shadow: 0 0 10px;
}
.avater_box {
  height: 130px;
  width: 130px;

  position: absolute;
  left: 50%;
  padding: 10px;
  transform: translate(-50%, -50%);
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    box-shadow: 0 0 10px;
    background-color: cornsilk;
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}
.login_from {
  position: absolute;
  bottom: 0;
  width: 90%;
  right: 20px;
  padding: 0 20px;
}
</style>